﻿Imports System.Data.SqlClient

Public Class frmQLKHChoThue

    Private Sub frmQLKHChoThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listKhachHangChoThue As New List(Of KhachHangChoThueDTO)

        listKhachHangChoThue = KhachHangChoThueBUS.GetAll()
        DataGridViewX1.DataSource = listKhachHangChoThue
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If Not txtHoTenChuNha.Text = "" Then
            If Not txtSoDienThoai.Text = "" Then
                If Not txtDiaChi.Text = "" Then
                    Dim sqlparameters(2) As SqlParameter
                    Dim QLKhachThue As KhachHangChoThueDTO = New KhachHangChoThueDTO()
                    QLKhachThue.strHoTen = txtHoTenChuNha.Text
                    QLKhachThue.strDiaChi = txtDiaChi.Text
                    QLKhachThue.strSoDienThoai = txtSoDienThoai.Text


                    Dim para0 As New SqlParameter("hoten", SqlDbType.NVarChar)
                    para0.Direction = ParameterDirection.Input
                    para0.Value = QLKhachThue.strHoTen
                    sqlparameters(0) = para0

                    Dim para1 As New SqlParameter("diachi", SqlDbType.NVarChar)
                    para1.Direction = ParameterDirection.Input
                    para1.Value = QLKhachThue.strDiaChi
                    sqlparameters(1) = para1

                    Dim para2 As New SqlParameter("sodienthoai", SqlDbType.VarChar)
                    para2.Direction = ParameterDirection.Input
                    para2.Value = QLKhachThue.strSoDienThoai
                    sqlparameters(2) = para2




                    KhachHangChoThueBUS.Insert(sqlparameters)
                    MessageBox.Show("Thêm Thành Công!!!!!")

                    Dim listKhachHangChoThue As New List(Of KhachHangChoThueDTO)

                    listKhachHangChoThue = KhachHangChoThueBUS.GetAll()
                    DataGridViewX1.DataSource = listKhachHangChoThue
                Else

                    MsgBox("Bạn chưa nhập vào địa chỉ  ", MsgBoxStyle.Critical, "Thông báo!")

                    txtDiaChi.Text = Focus()
                End If
            Else

                MsgBox("Bạn chưa nhập vào số điện thoại  ", MsgBoxStyle.Critical, "Thông báo!")

                txtSoDienThoai.Text = Focus()
            End If
        Else

            MsgBox("Bạn chưa nhập vào họ tên ", MsgBoxStyle.Critical, "Thông báo!")
            txtHoTenChuNha.Focus()

        End If
    End Sub



    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If Not txtHoTenChuNha.Text = "" Then
            If Not txtSoDienThoai.Text = "" Then
                If Not txtDiaChi.Text = "" Then
                    Dim sqlparameters(3) As SqlParameter
                    Dim QLKhachThue As KhachHangChoThueDTO = New KhachHangChoThueDTO()

                    QLKhachThue.iMaKhachHangChoThue = DataGridViewX1.CurrentRow.Cells("MaChuNha").Value
                    QLKhachThue.strHoTen = txtHoTenChuNha.Text
                    QLKhachThue.strDiaChi = txtDiaChi.Text
                    QLKhachThue.strSoDienThoai = txtSoDienThoai.Text


                    Dim para0 As New SqlParameter("makh", SqlDbType.Int)
                    para0.Direction = ParameterDirection.Input
                    para0.Value = QLKhachThue.iMaKhachHangChoThue
                    sqlparameters(0) = para0

                    Dim para1 As New SqlParameter("hoten", SqlDbType.NVarChar)
                    para1.Direction = ParameterDirection.Input
                    para1.Value = QLKhachThue.strHoTen
                    sqlparameters(1) = para1

                    Dim para2 As New SqlParameter("diachi", SqlDbType.NVarChar)
                    para2.Direction = ParameterDirection.Input
                    para2.Value = QLKhachThue.strDiaChi
                    sqlparameters(2) = para2

                    Dim para3 As New SqlParameter("sodienthoai", SqlDbType.VarChar)
                    para3.Direction = ParameterDirection.Input
                    para3.Value = QLKhachThue.strSoDienThoai
                    sqlparameters(3) = para3

                    KhachHangChoThueBUS.Update(sqlparameters)
                    MessageBox.Show("Chúc Mừng Bạn Sửa Thành CÔng!!!!!")
                    Dim listKhachHangChoThue As New List(Of KhachHangChoThueDTO)

                    listKhachHangChoThue = KhachHangChoThueBUS.GetAll()
                    DataGridViewX1.DataSource = listKhachHangChoThue

                Else

                    MsgBox("Bạn chưa nhập vào địa chỉ  ", MsgBoxStyle.Critical, "Thông báo!")

                    txtDiaChi.Text = Focus()
                End If
            Else

                MsgBox("Bạn chưa nhập vào số điện thoại  ", MsgBoxStyle.Critical, "Thông báo!")

                txtSoDienThoai.Text = Focus()
            End If
        Else

            MsgBox("Bạn chưa nhập vào họ tên ", MsgBoxStyle.Critical, "Thông báo!")
            txtHoTenChuNha.Focus()

        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim chunha As KhachHangChoThueDTO = New KhachHangChoThueDTO()


        chunha.iMaKhachHangChoThue = DataGridViewX1.CurrentRow.Cells("MaChuNha").Value


        Dim para1 As New SqlParameter("machunha", SqlDbType.Int)
        para1.Direction = ParameterDirection.Input
        para1.Value = chunha.iMaKhachHangChoThue


        KhachHangChoThueBUS.Delete(para1)
        MessageBox.Show("Chúc Mừng Bạn Xóa Thành CÔng!!!!!")
        Dim listKhachHangChoThue As New List(Of KhachHangChoThueDTO)

        listKhachHangChoThue = KhachHangChoThueBUS.GetAll()
        DataGridViewX1.DataSource = listKhachHangChoThue

    End Sub

    Private Sub DataGridViewX1_SelectionChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewX1.SelectionChanged
        txtHoTenChuNha.Text = DataGridViewX1.CurrentRow.Cells(1).Value
        txtDiaChi.Text = DataGridViewX1.CurrentRow.Cells(2).Value
        txtSoDienThoai.Text = DataGridViewX1.CurrentRow.Cells(3).Value
    End Sub


End Class